import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '../views/home/HomePage.vue'
import LoginView from '../views/user/UserLoginView.vue'
import UserRegisterView from "@/views/user/UserRegisterView.vue";
import store from '../stores/user.js'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "login",
      component: LoginView,
      meta:{
        requestAuth: false,
      }
    },
    {
      path:"/register",
      name:"register",
      component:UserRegisterView,
      meta:{
        requestAuth: false,
      }
    },
    {
      path: '/home',
      name: 'home',
      component: HomePage,
      redirect: '/home/totalscore',
      meta:{
        requestAuth: true,
      },
      children:[
        {
          path:'totalscore',
          name:'totalscore',
          component:()=>import('@/views/home/components/totalscore.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'zhibiao',
          name:'zhibiao',
          component:()=>import('@/views/home/components/zhibiao.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'ysylzy',
          name:'ysylzy',
          component:()=>import('@/views/home/components/ysylzy.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'zzyqk',
          name:'zzyqk',
          component:()=>import('@/views/home/components/zzyqk.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'byzhxlqk',
          name:'byzhxlqk',
          component:()=>import('@/views/home/components/byzhxlqk.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'cbjczl',
          name:'cbjczl',
          component:()=>import('@/views/home/components/cbjczl.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'gccch',
          name:'gccch',
          component:()=>import('@/views/home/components/gccch.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'gcctd',
          name:'gcctd',
          component:()=>import('@/views/home/components/gcctd.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'gyxljyhdcyqk',
          name:'gyxljyhdcyqk',
          component:()=>import('@/views/home/components/gyxljyhdcyqk.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'jxsp',
          name:'jxsp',
          component:()=>import('@/views/home/components/jxsp.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'jyyzyxgd',
          name:'jyyzyxgd',
          component:()=>import('@/views/home/components/jyyzyxgd.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'kysp',
          name:'kysp',
          component:()=>import('@/views/home/components/kysp.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'sxsjsxjdqk',
          name:'sxsjsxjdqk',
          component:()=>import('@/views/home/components/sxsjsxjdqk.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'tynl',
          name:'tynl',
          component:()=>import('@/views/home/components/tynl.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'xxcx',
          name:'xxcx',
          component:()=>import('@/views/home/components/xxcx.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'ylkcjs',
          name:'ylkcjs',
          component:()=>import('@/views/home/components/ylkcjs.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'zysy',
          name:'zysy',
          component:()=>import('@/views/home/components/zysy.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path: 'quanzhong',
          name: 'quanzhong',
          component: () => import('@/views/home/components/quanzhong.vue'),
          meta:{
            requestAuth: false,
          },
        },
        {
          path:'lqfs',
          name:'lqfs',
          component:()=>import('@/views/home/components/lqfs.vue'),
          meta:{
            requestAuth: false,
          },
        },
        ]
    },

    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      redirect: { name: 'login' } // 使用命名路由更安全
    },
  ],
})
router.beforeEach((to, from, next) => {
  // 不需要认证的路由白名单
  const allowList = ['login', 'register', 'totalscore'];

  // 如果在白名单中，直接放行
  if (allowList.includes(to.name)) {
    return next();
  }

  // 需要认证但未登录，跳转到登录页
  if (!store.state.is_login) {
    return next({ name: "login" });
  }

  // 其他情况放行
  next();
});
export default router
